package com.huiyujinrong.qiangdanbao.activity.fragment;

import android.content.Context;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.OderBean;
import com.huiyujinrong.qiangdanbao.model.OderModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class FragmentOrderPresenter implements FragmentOrderContract.Presenter{


    @Inject
    OderModel model;
    private FragmentOrderContract.View view;
    private Context context;

    public FragmentOrderPresenter(FragmentOrderContract.View view,Context context){
        this.view=view;
        this.context=context;
        setDagger2();
    }

    private void setDagger2(){
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    @Override
    public void getOrder(String user_id,int currentPage,int status) {
        model.getOderList(user_id ,currentPage,status);
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEventList(RespEvent<OderBean> event) {
        if (event.getUrl().equals(Api.getOderData)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.setData(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if(event.isAlert()){
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
